import axios from 'axios'

const btn = document.getElementById('btn');
btn.onclick = async (e) => {
  // 阻止事件的默认行为 => 不要提交表单
  e.preventDefault();

  // 收集数据
  const name = document.getElementById('name').value;
  const author = document.getElementById('author').value;
  const price = document.getElementById('price').value;
  // 提交添加图书的ajax请求
  await axios({
    url: 'http://localhost:8080/book',
    method: 'POST',
    data: { name, author, price },
  });
  // 成功后, 跳转到books页面
  window.location = '/books.html';
};
